package service

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"strings"
)

type _roleService struct{}

func (s _roleService) CreateApi(ctx context.Context, enterpriseId int64, name string, describe string) error {
	_, err := dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId), dao.RoleEntity.Name.Eq(name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("名字已经存在,不能重复")
	}
	if err = dao.RoleEntity.WithContext(ctx).Create(&model.RoleEntity{
		Name:         name,
		Describe:     describe,
		EnterpriseID: enterpriseId,
	}); err != nil {
		return err
	}
	return nil
}

func (s _roleService) CreateBatchApi(ctx context.Context, tx *dao.QueryTx, model []*model.RoleEntity) error {
	if err := tx.RoleEntity.WithContext(ctx).CreateInBatches(model, len(model)); err != nil {
		return err
	}
	return nil
}
func (s _roleService) DeleteByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId), dao.RoleEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _roleService) GetRoleListApi(ctx context.Context, enterpriseId int64) ([]*model.RoleEntity, error) {
	return dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId)).Find()
}

func (s _roleService) GetRolePageApi(ctx context.Context, enterpriseId int64, name string, pageNumber int64, pageSize int64) (result []*model.RoleEntity, count int64, err error) {
	tx := dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId))
	if name != "" {
		name = strings.ReplaceAll(name, "%", "\\%")
		name = strings.ReplaceAll(name, "_", "\\%")
		tx = tx.Where(dao.RoleEntity.Name.Like("%" + name + "%"))
	}
	offset, limit := utils.DbPage(pageNumber, pageSize)
	return tx.Order(dao.RoleEntity.ID.Desc()).FindByPage(offset, limit)
}

func (s _roleService) ModifyByIdApi(ctx context.Context, enterpriseId, id int64, name string, describe string) error {
	roleEntity, err := dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId), dao.RoleEntity.Name.Eq(name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) && roleEntity.ID != id {
		return errors.New("名字已经存在,不能重复")
	}
	if _, err = dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.ID.Eq(id)).UpdateSimple(
		dao.RoleEntity.Name.Value(name),
		dao.RoleEntity.Describe.Value(describe),
	); err != nil {
		return err
	}
	return nil
}

func (s _roleService) GetByIdApi(ctx context.Context, id int64) (*model.RoleEntity, error) {
	return dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.ID.Eq(id)).First()
}

func (s _roleService) GetByNameApi(ctx context.Context, enterpriseId int64, name string) ([]*model.RoleEntity, error) {
	tx := dao.RoleEntity.WithContext(ctx).Where(dao.RoleEntity.EnterpriseID.Eq(enterpriseId))
	if name != "" {
		tx = tx.Where(dao.RoleEntity.Name.Eq(name))
	}
	return tx.Find()
}

func (s _roleService) GetByCodeApi(ctx context.Context, enterpriseId int64, roleCode string) (*model.RoleEntity, error) {
	return dao.RoleEntity.WithContext(ctx).Where(
		dao.RoleEntity.EnterpriseID.Eq(enterpriseId),
		dao.RoleEntity.RoleCode.Eq(roleCode),
	).First()
}

var RoleService = new(_roleService)
